﻿Public Class Gallery
    Inherits System.Web.UI.Page
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
       
    End Sub
    

    Protected Sub btn_upload_image_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_upload_image.Click
        Dim uniID As Integer = Request.QueryString("UID")
        Dim U_id As Integer
        Dim Uname As String = Page.User.Identity.Name
        U_id = CInt(userID(Uname))
        If ful_browse.HasFile Then
            ful_browse.SaveAs(Server.MapPath("Gallery" & "/" & ful_browse.FileName))
        End If
        Dim sql As String = " INSERT INTO Gallery (UniversityID, Photos, Description, G_UserID) VALUES (@a1,@a2,@a3,@a4)"
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("UniConnectionString").ConnectionString)
        Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(sql, conn)

        cmd.Parameters.AddWithValue("@a1", uniID)
        cmd.Parameters.AddWithValue("@a2", "Gallery/" & ful_browse.FileName)
        cmd.Parameters.AddWithValue("@a3", txtB_Description.Text)
        cmd.Parameters.AddWithValue("@a4", U_id)

        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
        Response.Redirect("List.aspx")
    End Sub
    Protected Function userID(ByVal uname As String) As Integer
        Dim uID As Integer
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("UniConnectionString").ConnectionString)
        conn.Open()
        Dim IDsql As String = " select aspnet_Users.UserId from aspnet_Users Where aspnet_Users.UserName = '" & uname & "'"
        Dim IDcmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(IDsql, conn)
        Dim Dreader As OleDb.OleDbDataReader
        Try
            Dreader = IDcmd.ExecuteReader()
            Dreader.Read()
            uID = Dreader("UserId")
        Catch
        End Try
        Return uID
        conn.Close()
        IDcmd.Dispose()
        conn.Dispose()
    End Function

    
End Class